var gulp = require("gulp");
var esbuild = require('gulp-esbuild');
var browserSync = require('browser-sync').create();

var paths = {
  pages: ['src/*.html'],
  ts: ['src/**/*.ts']
};

// Copy HTML files
const copyHtml = function () {
  return gulp.src(paths.pages)
    .pipe(gulp.dest("dist"));
}

// Bundle TypeScript files
const build = function () {
  return gulp.src('src/main.ts')
    .pipe(esbuild({
      bundle: true,
      minify: true,
      sourcemap: true,
      target: ['es2015'],
      outfile: 'bundle.js',
    }))
    .pipe(gulp.dest("dist"));
}

gulp.task("copy-html", copyHtml);
gulp.task("build", build);
gulp.task("default", gulp.series('copy-html', 'build'));

// Watch files for changes and recompile
gulp.task("watch", gulp.series("default", function () {
  browserSync.init({
    server: {
      baseDir: './dist'
    }
  });

  gulp.watch(paths.ts, gulp.series(build, browserSync.reload));
  gulp.watch(paths.pages, gulp.series(copyHtml, browserSync.reload));
}));




